﻿#include "HnrjitRadioBox.h"
#include "HnrjitContainer.h"

CHnrjitRadioBox::CHnrjitRadioBox()
	: CHnrjitButton()
	, m_isChecked(FALSE)
{
	m_nTextAlign |= DT_VCENTER;
}

void CHnrjitRadioBox::SetAttribute(const TiXmlElement* pxElement, HZIP hZipRes /*= NULL*/)
{
	CHnrjitButton::SetAttribute(pxElement, hZipRes);

	const CHAR* pszValue;
	if (pszValue = pxElement->Attribute("Checked"))
	{
		if (strcmp(pszValue, "true") == 0)
		{
			SetChecked(TRUE);
		}
	}
}
CHnrjitControl::ControlStatus CHnrjitRadioBox::GetControlState() const
{
	return (m_isEnabled && m_pContainer->IsEnabled()) ? ((m_isTriggered || m_isChecked) ? kActivated : kNormal) : kDisabled;
}

BOOL CHnrjitRadioBox::IsChecked() const
{
	return m_isChecked;
}

void CHnrjitRadioBox::SetChecked(BOOL isChecked)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	if (m_isChecked != isChecked)
	{
		m_isChecked = isChecked;
		Invalidate(m_rcGeometry);
		if (m_isChecked)
		{
			// 如果勾选当前单选按钮，则取消其他单选按钮勾选状态
			for (size_t i = 0; i < m_pContainer->GetControlCount(); i++)
			{
				if (m_pContainer->GetControl(i) != this && m_pContainer->GetControl(i)->IsKindOf(CLSID_HNRJITRADIOBOX))
				{
					CHnrjitRadioBox* pRadioBox = static_cast<CHnrjitRadioBox*>(m_pContainer->GetControl(i));
					pRadioBox->m_isChecked = FALSE;
					pRadioBox->Invalidate(pRadioBox->m_rcGeometry);
				}
			}
		}
	}
}

void CHnrjitRadioBox::DrawControl(HDC hdc)
{
	ImageInfo tagInfo;
	CRect rcReference = m_isTriggered ? CRect(m_rcGeometry.left + 1, m_rcGeometry.top + 1,
		m_rcGeometry.right + 1, m_rcGeometry.bottom + 1) : m_rcGeometry;
	rcReference.MoveToXY(GetLocation() + m_pContainer->GetLocation());

	ControlStatus iControlStatus = GetControlState();
	if (m_strTitle.GetLength())
	{
		DrawRect(hdc, rcReference, iControlStatus);

		if (m_spBackIcons[iControlStatus] && m_spBackIcons[iControlStatus]->GetImageInfo(&tagInfo) == S_OK)
		{
			long x, y, w, h;
			w = tagInfo.Width;
			h = tagInfo.Height;
			x = rcReference.left + 3;
			y = (rcReference.top + rcReference.bottom - w) / 2;; // rcReference.top + (rcReference.Height() - tagInfo.Height) / 2;

			CRect rcPicture = CRect(CPoint(x, y), CSize(w, h));
			DrawImage(hdc, rcPicture, iControlStatus);

			// 图片两侧及最右侧留三个像素
			x = rcReference.left + w + 6; // rcReference.left + rcPicture.Width() + 6;
			y = rcReference.top;
			w = rcReference.right - x - 3; // rcReference.Width() - (rcPicture.Width() + 6 - 3);
			h = rcReference.Height();

			CRect rcText = CRect(CPoint(x, y), CSize(w, h));
			DrawTitle(hdc, rcText, iControlStatus);
		}
		else
		{
			DrawTitle(hdc, rcReference, iControlStatus);
		}
	}
	else
	{
		if (m_spBackIcons[iControlStatus])
		{
			DrawImage(hdc, rcReference, iControlStatus);
		}
		else
		{
			DrawRect(hdc, rcReference, iControlStatus);
		}
	}
}

void CHnrjitRadioBox::OnMousePress(UINT nFlags, CPoint point)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	if (!m_isTriggered)
	{
		m_isTriggered = TRUE;
		Invalidate(m_rcGeometry);
	}
}

void CHnrjitRadioBox::OnMouseRelease(UINT nFlags, CPoint point)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	if (m_isTriggered)
	{
		m_isTriggered = FALSE;
		if (m_pContainer->GetActivate() != this || !m_isChecked)
			SetChecked(!m_isChecked);
		OnClicked();
	}
}
